/**
 * Created by xiongwei on 2017/5/6.
 */

import LogService from "utils"
import AppServer from '../common/AppService';
import HttpService from './HttpService';
function successHandler(resp, resolve, reject) {
  const code = resp.code;
  const msg = resp.msg;
  if (code === 0 || code === '0000' || code === '000000') {
    resolve(resp.objList || resp.obj);
  } else {
    const err = new Error(msg);
    err.code = code;
    err.obj = resp.obj;
    reject(err);
  }
};
  /**
   * Get
   * @param apiPath
   * @param headers
   * @param params
   * @param server
   * @returns {Promise.<TResult>}
   */
  export async function commonGet(apiPath, headers, params, server = AppServer.baseUrl) {
    const url = `${server}${apiPath.indexOf('/') === 0 ? apiPath.substring(1) : apiPath}`;
    const dataServer = false;
     LogService.debug('request.get:', url, headers, params);
    return new Promise((resolve, reject) => {
      HttpService.doGet(url, headers, params)
        .then(response => response.json())
        .then((resp) => {
          LogService.debug(apiPath, resp);
          successHandler(resp, resolve, reject, dataServer);
        })
        .catch((err) => {
          LogService.warn(url, err);
          reject(err);
        });
    });
  };

  /**
   * 客户端通用的网络请求
   * @param apiPath
   * @param headers
   * @param params
   * @param server
   * @returns {Promise}
   */
  export async function commonFormPost(apiPath, headers, params, server = AppServer.baseUrl) {

    const url = `${server}${apiPath.indexOf('/') === 0 ? apiPath.substring(1) : apiPath}`;
    // const {loginUser} = UserService;
    // if (!!params && !!loginUser) {
    //   Object.assign(params, {userId: loginUser.userId, token: loginUser.token});
    // }
    LogService.debug('request.form.post:', url, headers, params);
    return new Promise((resolve, reject) => {
      HttpService.doFormPost(url, headers, params)
        .then(response => response.json())
        .then((resp) => {
           LogService.debug(apiPath, resp);
          successHandler(resp, resolve, reject);
        })
        .catch((err) => {
          // LogService.warn(url, err);
          reject(err);
        });
    });
  };

  /**
   * REST形式的POST请求
   * @param apiPath
   * @param headers
   * @param params
   * @param server
   * @returns {Promise.<TResult>}
   */
  export async function commonRestPost(apiPath, headers, params, server = AppServer.baseUrl) {
    const url = `${server}${apiPath.indexOf('/') === 0 ? apiPath.substring(1) : apiPath}`;
    // const {loginUser} = UserService;
    // if (!!params && !!loginUser) {
    //   Object.assign(params, {userId: loginUser.userId, token: loginUser.token});
    // }
    // LogService.debug('request.rest.post:', url, headers, params);
    return new Promise((resolve, reject) => {
      HttpService.doRestPost(url, headers, params)
        .then(response => response.json())
        .then((resp) => {
          // LogService.debug(apiPath, resp);
          successHandler(resp, resolve, reject);
        })
        .catch((err) => {
          // LogService.warn(url, err);
          reject(err);
        });
    });
  };

